package com.pn.service.impl;

import com.pn.entity.InStore;
import com.pn.entity.Purchase;
import com.pn.entity.Result;
import com.pn.mapper.InStoreMapper;
import com.pn.page.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.pn.mapper.PurchaseMapper;
import com.pn.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    private PurchaseMapper purchaseMapper;
    @Resource
    private InStoreMapper inStoreMapper;

    @Override
    public Result addPurchase(Purchase purchase) {

        int result = purchaseMapper.insertPurchase(purchase);

        if (result > 0) {
            return Result.ok("添加采购单成功！");
        }

        return Result.err(Result.CODE_ERR_BUSINESS, "添加采购单失败！");
    }

    @Override
    public Page queryPurchaseByPage(Page page, Purchase purchase) {

        List<Purchase> purchaseRows = purchaseMapper.selectPurchaseByPage(page, purchase);

        int purchaseRowCount = purchaseMapper.countPurchase(purchase);

        page.setResultList(purchaseRows);
        page.setTotalNum(purchaseRowCount);

        return page;
    }

    @Override
    public Result removePurchase(Integer buyId) {
        int i = purchaseMapper.deletePurchase(buyId);
        if (i > 0) {
            return Result.ok("删除入库单成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "删除入库单失败！");
    }

    @Override
    public Result setPurchase(Purchase purchase) {

        int i = purchaseMapper.updatePurchase(purchase);

        if (i > 0) {
            return Result.ok("修改入库单成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "修改采购单失败！");
    }
}
